package com.wcar.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wcar.common.entity.FrontUser;
import com.wcar.common.entity.LoginLog;
import com.wcar.common.util.WebUtil;
import com.wcar.system.dao.FrontUserDao;
import com.wcar.system.dao.LoginLogDao;
import com.wcar.system.dao.UserDao;
import com.wcar.common.entity.User;
import com.wcar.system.feign.WcarUserFeign;
import com.wcar.system.service.UserService;
import com.wcar.common.vo.Result;
import com.wcar.system.vo.PassVo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;
    @Resource
    private FrontUserDao frontUser;
    @Resource
    private LoginLogDao loginLogDao;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private WcarUserFeign wcarUserFeign;
    @Override
    public Result<User> login(String name) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("user_name",name);
        wrapper.eq("status",0);
        wrapper.eq("del_flag",0);
        User user = userDao.selectOne(wrapper);
        HttpServletRequest request = WebUtil.getRequest();
        user.setLoginIp(request.getRemoteAddr());
        user.setLoginDate(new Date());
        userDao.updateById(user);
        if(user==null){
            return new Result<>(500,"账号或密码错误",null);
        }
        return new Result<>(200,"登陆成功",user);
    }

    @Override
    public Result<User> selectById(Integer id) {
        User user = userDao.selectById(id);
        return new Result<>(200,"获取用户成功",user);
    }

    @Override
    public Result<List<User>> selectAll() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        List<User> users = userDao.selectList(wrapper);
        return new Result<>(200,"查询所有用户信息成功",users);
    }

    @Override
    public Result<User> modifyPass(PassVo pass) {
        String token = WebUtil.getToken();
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get("user:login:" + token);
        User user1 = JSON.parseObject(s, User.class);
        Md5Hash md5 = new Md5Hash(pass.getPass(),user1.getSalt(),5);
        user1.setPassword(md5.toString());
        int i = userDao.updateById(user1);
        return new Result<>(200,"密码修改成功",null);
    }

    @Override
    public Result<FrontUser> fontLogin(String name) {
        QueryWrapper<FrontUser> wrapper=new QueryWrapper<>();
        wrapper.eq("phone",name);
        wrapper.eq("status",0);
        FrontUser frontUser1 = frontUser.selectOne(wrapper);
        System.out.println(frontUser1);
        if(frontUser1==null){
            return new Result<>(500,"账号密码错误",null);
        }
        //!!!!!添加日志
        String remoteAddr = WebUtil.getRequest().getRemoteAddr();
        LoginLog loginLog = new LoginLog();
        loginLog.setLoginName(frontUser1.getUserName());
        loginLog.setIpaddr(remoteAddr);
        loginLog.setLoginLocation("河南郑州");
        loginLog.setBrowser("ie");
        loginLog.setOs("window");
        loginLog.setStatus("0");
        loginLog.setLoginTime(new Date());
        loginLog.setUserId(frontUser1.getUserId());
        wcarUserFeign.addLoginLog(loginLog);
        return new Result<>(200,"登陆成功",frontUser1);
    }
}
